package com.arsl.client.view;

import com.arsl.client.util.Constants;
import com.arsl.client.util.Constants.ContentStatus;
import com.arsl.client.util.Utils;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ContentInputWidget extends Composite implements Validatable {

	private static ContentInputPanelUiBinder uiBinder = GWT
			.create(ContentInputPanelUiBinder.class);

	interface ContentInputPanelUiBinder extends
			UiBinder<Widget, ContentInputWidget> {
	}

	public ContentInputWidget() {
		initWidget(uiBinder.createAndBindUi(this));
		cmbType.addItem(Constants.VIDEO_CONTENT_TYPE);
		cmbType.addItem(Constants.WEB_PAGE_CONTENT_TYPE);
		cmbType.addItem(Constants.CALL_CONTENT_TYPE);
		cmbType.addItem(Constants.SMS_CONTENT_TYPE);
		updateOnType();
	}

	String contentUUID;
	Integer contentId;
	ContentStatus contentStatus;

	public void setContentUUID(String uniqueId) {
		this.contentUUID = uniqueId;
	}

	public String getContentUUID() {
		return this.contentUUID;
	}

	public void setContentStatus(ContentStatus contentStatus) {
		this.contentStatus = contentStatus;
	}

	public ContentStatus getContentStatus() {
		return this.contentStatus;
	}

	public void setContentId(Integer contentId) {
		this.contentId = contentId;
	}

	public Integer getContentId() {
		return this.contentId;
	}

	@UiField
	TextBox txtTitle;

	@UiField
	TextArea txtDesc;

	@UiField
	TextBox txtUrl;

	@UiField
	ListBox cmbType;

	@UiField
	Label lblDesc;

	@UiField
	Label lblNumber;

	@UiField
	Label lblUrl;

	@UiField
	TextBox txtNumber;

	@UiHandler("cmbType")
	public void onTypeChange(ChangeEvent e) {
		updateOnType();
	}

	private void updateOnType() {
		if (Constants.CALL_CONTENT_TYPE.equalsIgnoreCase(cmbType
				.getItemText(cmbType.getSelectedIndex()))
				|| Constants.SMS_CONTENT_TYPE.equalsIgnoreCase(cmbType
						.getItemText(cmbType.getSelectedIndex()))) {
			lblNumber.setVisible(true);
			txtNumber.setVisible(true);
			lblUrl.setVisible(false);
			txtUrl.setVisible(false);
		} else {
			lblNumber.setVisible(false);
			txtNumber.setVisible(false);
			lblUrl.setVisible(true);
			txtUrl.setVisible(true);
		}
		if (Constants.CALL_CONTENT_TYPE.equalsIgnoreCase(cmbType
				.getItemText(cmbType.getSelectedIndex()))) {
			lblDesc.setVisible(false);
			txtDesc.setVisible(false);
		} else {
			lblDesc.setVisible(true);
			txtDesc.setVisible(true);
		}
		if (Constants.SMS_CONTENT_TYPE.equalsIgnoreCase(cmbType
				.getItemText(cmbType.getSelectedIndex()))) {
			lblDesc.setText("Message");
		} else {
			lblDesc.setText("Description");
		}

	}

	public String getTitle() {
		return txtTitle.getText();
	}

	public void setTitle(String title) {
		txtTitle.setText(title);
	}

	public String getDescription() {
		if (Constants.SMS_CONTENT_TYPE.equalsIgnoreCase(cmbType
				.getItemText(cmbType.getSelectedIndex()))) {
			return txtNumber.getText() + "," + txtDesc.getText();
		}
		if (Constants.CALL_CONTENT_TYPE.equalsIgnoreCase(cmbType
				.getItemText(cmbType.getSelectedIndex()))) {
			return txtNumber.getText();
		}
		return txtDesc.getText();
	}

	public void setDescription(String description) {
		if (Constants.SMS_CONTENT_TYPE.equalsIgnoreCase(getType())) {
			txtNumber.setText(description.split(",")[0]);
			txtDesc.setText(description.split(",")[1]);
			return;
		}
		if (Constants.CALL_CONTENT_TYPE.equalsIgnoreCase(getType())) {
			txtNumber.setText(description);
			return;
		}
		txtDesc.setText(description);
	}

	public String getType() {
		return cmbType.getItemText(cmbType.getSelectedIndex());
	}

	public void setType(Integer selectedIndex) {
		cmbType.setSelectedIndex(selectedIndex);
		updateOnType();
	}

	public void setType(String type) {

		for (int i = 0; i < cmbType.getItemCount(); i++) {
			if (type.equalsIgnoreCase(cmbType.getItemText(i))) {
				cmbType.setSelectedIndex(i);
				break;
			}
		}
		updateOnType();
	}

	public String getUrl() {
		return txtUrl.getText();
	}

	public void setUrl(String url) {
		txtUrl.setText(url);
	}

	@Override
	public boolean isValid() {
		if (Constants.CALL_CONTENT_TYPE.equalsIgnoreCase(getType())
				|| Constants.SMS_CONTENT_TYPE.equalsIgnoreCase(getType())) {
			return !(Utils.isNullOrEmpty(getTitle()) || Utils
					.isNullOrEmpty(getDescription()));
		}
		if (Utils.isNullOrEmpty(getTitle()) || Utils.isNullOrEmpty(getType())
				|| Utils.isNullOrEmpty(getUrl())) {
			return false;
		}
		return true;
	}
}
